package com.css.fxfzmh.zcpt.sys.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.css.fxfzmh.util.PlatformObjectUtils;
import com.css.fxfzmh.zcpt.sys.entity.MessageEntity;
import com.css.fxfzmh.zcpt.sys.entity.SystemMenu;
import com.css.fxfzmh.zcpt.sys.entity.vo.MessageVo;
import com.css.fxfzmh.zcpt.sys.service.SysMenuService;
import com.css.fxfzmh.util.RestTemplateUtil;
import org.slw.common.helper.PropsHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class SysMenuServiceImpl implements SysMenuService {

    @Autowired
    private RestTemplateUtil restTemplateUtil;


    @Override
    public String getMenuListByUserId(String userId) {
        //读取配置文件
        Properties configGroup = PropsHelper.loadProps("slw-config.properties");
        String sysId = configGroup.getProperty("app.system.id");
        String url = configGroup.getProperty("baseRestUrl") + "/smenu_dzj/getMenusByUserId?userId=" + userId + "&sysId=" + sysId;
        Map<String, Object> paramMap = new HashMap<>(2);
        paramMap.put("userId", userId);
        paramMap.put("sysId", sysId);
        ResponseEntity<String> responseEntity = restTemplateUtil.sendHttpRequest(url, paramMap);
        return responseEntity.getBody();
    }

    @Override
    public List<MessageVo> getMessageByUserId(String userId) {
        List<MessageVo> list = new ArrayList<>();
        Properties configGroup = PropsHelper.loadProps("slw-config.properties");
        String sysId = configGroup.getProperty("app.system.id");
        String url = configGroup.getProperty("baseRestUrl") + "/smenu_dzj/getMenusByUserId?userId=" + userId + "&sysId=" + sysId;
        Map<String, Object> paramMap = new HashMap<>(2);
        paramMap.put("userId", userId);
        paramMap.put("sysId", sysId);
        ResponseEntity<String> responseEntity = restTemplateUtil.sendHttpRequest(url, paramMap);
        List<SystemMenu> menuList = JSONObject.parseArray(responseEntity.getBody(), SystemMenu.class);
        if(PlatformObjectUtils.isEmpty(menuList)){
            throw new NullPointerException("该用户没有任何权限，请联系管理员!");
        }
        for (SystemMenu item : menuList) {
            List<SystemMenu> children = item.getChildren();
            if (!PlatformObjectUtils.isEmpty(children)) {
                for (SystemMenu item1 : children) {
                    List<SystemMenu> children1 = item1.getChildren();
                    if (!PlatformObjectUtils.isEmpty(children1)) {
                        for (SystemMenu item2 : children1) {
                            if (!PlatformObjectUtils.isEmpty(item2.getDescription())) {
                                MessageVo messageVo = getMessageData(item2.getDescription(), item2.getMeta().getTitle(),item2.getMenuType());
                                list.add(messageVo);
                            }
                        }
                    }
                }
            }
        }
        return list;
    }


    private MessageVo getMessageData(String url, String title,String jumpPath) {

        Map<String, Object> paramMap = new HashMap<>();
        ResponseEntity<String> cchResponseEntity = restTemplateUtil.sendHttpRequest(url, paramMap);

        List<MessageEntity> messageEntities = JSON.parseArray(cchResponseEntity.getBody(), MessageEntity.class);

        MessageVo messageVo = new MessageVo();
        messageVo.setSystemName(title);
        messageVo.setMessageNumber(messageEntities.size() + "");
        messageVo.setJumpPath(jumpPath);
        messageVo.setList(messageEntities);
        return messageVo;

    }
}
